<?php

declare(strict_types=1);

namespace App\Http\Controllers\Manage;

use App\Repositories\FileRepository;

class ManageController extends ManageBaseController
{
    /**
     * ActivityRepository
     * @var
     */
    private $activityPep;

    /**
     * 活动Id
     * @var
     */
    private $activityId;

    /**
     * @var ActivitySignupRepository
     */
    private $signupRep;

    /**
     * 活动基础数据
     */
    private $activityData;

    /**
     * FileRepository
     */
    private $fileRep;

    /**
     * ManageController constructor.
     */
    public function __construct()
    {
        parent::__construct();

        $this->activityPep = new ActivityRepository();
        $this->signupRep = new ActivitySignupRepository();
        $this->fileRep = new FileRepository();

        $activityId = (int)request()->get('activity_id', 0);

        if ($activityId) {
            $this->activityId = $activityId;
            $activity = $this->activityPep->getOneActivity($activityId);
            $this->activityData = $activity;
            view()->share('data', $this->activityData);
        }
    }

    /**
     * 概况
     */
    public function index()
    {
        return view('manage.manage.index', [
            'active' => 'index'
        ]);
    }

    /**
     * 志愿者管理
     */
    public function volunteer()
    {
        //Tab 切换
        $tab = request()->get('tab', 1);
        //检索条件
        $name = request()->get('name', '');
        $gender = request()->get('gender', '');
        $province = request()->get('province', '');
        $roleName = request()->get('role_name', '');
        $skill = request()->get('skill', '');
        $roleId = request()->get('role_id', 0);
        $annex = request()->get('annex', '');
        $order = request()->get('order', '');
        $status = request()->get('status', 0);

        //Tab 数据统计
        $tabData = $this->signupRep->getSignupStatusTotalByActivityId($this->activityId, $status);
        //检索的列表
        $map = [
            'tab' => $tab,
            'status' => $status,
            'name' => $name,
            'gender' => $gender,
            'role_name' => $roleName,
            'province' => $province,
            'skill' => $skill,
            'annex' => $annex,
            'role_id' => $roleId,
            'order' => $order,
            'activity_id' => $this->activityId
        ];
        $volunteerList = $this->signupRep->getActivityVolunteerList($map);

        return view('manage.manage.volunteer', [
            'active' => 'volunteer',
            'tab' => $tab,
            'tab_data' => $tabData,
            'map' => $map,
            'volunteer_list' => $volunteerList,
            'province_list' => getChinaProvince()
        ]);
    }

    /**
     * 编辑活动报名的信息
     */
    public function editSignupInfo()
    {
        //接受参数
        $id = request()->get('sign_id', 0);
        $field = request()->get('field', '');
        $value = request()->get('val', '');
        if (empty($id) || empty($field) || empty($value)) {
            return response()->json([
                ['code' => 400, 'messages' => 'error', 'data' => []]
            ]);
        }

        $this->signupRep->editActivitySignupInfo([
            'id' => $id,
            'field' => $field,
            'value' => $value,
        ]);

        $data = ['code' => 200, 'messages' => 'success', 'data' => ['id' => $id, 'data' => $value]];
        return response()->json($data);
    }

    /**
     * 事件类型 event
     */
    public function setActivitySingUpAttribute()
    {
        $event = request()->get('event', 0);
        if (empty($event)) {
            return response()->json(['data' => '事件错误']);
        }
        $signId = request()->get('sign_id', '');
        $signIdString = trim($signId, ',');
        $activityRole = $this->activityData['activity_role'];
        $title = $msg = "";
        $status = 0;
        // 1:入选 批量入选
        if ($event == 1) {
            if (empty($signId)) {
                return response()->json(['data' => '报名Id或事件错误']);
            }

            $signIdArr = explode(',', $signIdString);
            $signInfo = $this->signupRep->getOneSignInfo((int)$signIdArr[0]);
            $title = '正在为 ' . $signInfo['contact']['name'] . ' 等 ' . count($signIdArr) . '位报名者 安排岗位';
            $status = ActivitySignup::SIGNUP_PASS;
            $msg = "恭喜您入选本活动志愿服务人员，感谢支持！";
            // 2:落选 全部落选
        } else if ($event == 2) {
            $tabData = $this->signupRep->getSignupStatusTotalByActivityId($this->activityId);
            if ($signIdString) {
                $signInfo = $this->signupRep->getOneSignInfo((int)$signIdString);
                $title = "志愿者 " . $signInfo['contact']['name'] . " 将落选";
            } else {
                $title = $tabData['tab_1'] . "名志愿者将落选";
            }

            $status = ActivitySignup::SIGNUP_REJECT;
            $msg = "本次志愿者筛选已完成，期待下次与您相见！";
            // 改为入选
        } else if ($event == 3) {
            if (empty($signId)) {
                return response()->json(['data' => '报名Id或事件错误']);
            }
            $signInfo = $this->signupRep->getOneSignInfo((int)$signIdString);
            $title = '正在为 ' . $signInfo['contact']['name'] . ' 安排岗位';
            $status = ActivitySignup::SIGNUP_PASS;
        }

        $html = view('manage.manage.batch_set_role', [
            'activity_role' => $activityRole,
            'title' => $title,
            'event' => $event,
            'status' => $status,
            'msg' => $msg,
            'sign_id' => $signIdString,
            'activity_id' => $this->activityId
        ])->render();

        return response()->json(['data' => $html]);
    }

    /**
     * 管理员给志愿者安排岗位
     * @return mixed
     */
    public function saveRole()
    {
        $signId = request()->get('sign_id', '');
        $roleId = request()->get('role_id', 0);
        $status = request()->get('status', 0);
        $activityId = request()->get('activity_id', 0);
        $event = request()->get('event', 0);

        if (empty($status) || empty($activityId)) {
            session()->put('error', '保存岗位错误');
            return redirect('/manage/volunteer?activity_id=' . $activityId);
        }

        //管理员给志愿者批量安排岗位
        $ret = $this->signupRep->saveRole([
            'sign_id' => $signId,
            'role_id' => $roleId,
            'status' => $status,
            'activity_id' => $activityId,
            'event' => $event
        ]);

        //发送消息
        $messages = request()->get('messages', '');
        if ($messages) {
            $signId = explode(',', $signId);
            $send = new SendMessagesVolunteerService();
            $send->sendMessagesVolunteer($signId, $this->activityData, (int)$event, $messages);
        }

        if (!$ret) {
            session()->put('error', '操作失败');
        } else {
            session()->put('success', '操作成功');
        }

        return redirect('/manage/volunteer?activity_id=' . $activityId);
    }

    /**
     * 确定选入/不予通过
     */
    public function promoted()
    {
        $signId = request()->get('sign_id', 0);
        $status = request()->get('status', 0);
        $activityId = request()->get('activity_id', 0);
        $roleId = request()->get('role_id', 0);

        if (empty($signId) || empty($status) || empty($activityId)) {
            session()->put('error', '保存岗位错误');
            return redirect('/manage/volunteer?activity_id=' . $activityId);
        }

        $ret = $this->signupRep->saveRole([
            'sign_id' => $signId,
            'role_id' => $roleId,
            'status' => $status,
            'activity_id' => $activityId,
        ]);

        if (!$ret) {
            session()->put('error', '操作失败');
        } else {
            session()->put('success', '操作成功');
        }

        return redirect('/manage/volunteer?activity_id=' . $activityId);
    }

    /**
     * 查看志愿者报名是上传的附件
     */
    public function showAttachment()
    {
        $signId = request()->get('sign_id', 0);
        $activityId = request()->get('activity_id', 0);
        if (empty($signId)) {
            session()->put('error', '保存岗位错误');
            return redirect('/manage/volunteer?activity_id=' . $activityId);
        }

        //附件
        $fileAttachment = $this->fileRep->getFilesByObject(ActivitySignup::SIGNUP_FILE, $signId);
        $html = view('manage.manage.attachment', [
            'file_attachment' => $fileAttachment
        ])->render();

        return response()->json(['data' => $html]);
    }

    /**
     * 附件下载
     */
    public function fileDownload()
    {
        $md5 = request()->get('md5');
        if (!$md5) {
            session()->put('error', '下载失败');
            $ref = Request()->server('HTTP_REFERER');
            return redirect($ref);
        }

        //开发下载
        $this->fileRep->fileDownload($md5);
    }
}
